<?php

require_once 'SQLConnector.php';
require_once '/../utility_definition/errorcode.php';
class Account {
    public $accName;
    public $password;
    public $empStatus;

    public static function init($input) {
        global $pdoconn;
        if (empty($input)) {
            $state = $pdoconn -> prepare("SELECT accName, password, empStatus FROM Account JOIN Employees ON Account.empID = Employees.empID");
            $state -> execute();
            return $state -> fetchAll(PDO::FETCH_OBJ);

        } else if ($input[':accName']){
            $state = $pdoconn -> prepare("SELECT accName, password, empStatus FROM Account JOIN Employees ON Account.empID = Employees.empID WHERE accName = :accName");
            $state -> execute(array(':accName' => $input[':accName']));
            if ($state ->rowCount() == 1)
                return $state->fetchAll(PDO::FETCH_CLASS, "Account");
        }
        return new Account();
    }

    public function presence() {
        global $pdoconn;
        $state = $pdoconn->prepare("SELECT accName, password FROM Account WHERE accName = :accName");
        $state -> execute(array(':accName' => $this->accName));
        if ($state->rowCount() ==1) {
            return true;
        } else {
            return false;
        }
    }

    public function save(){
        global $pdoconn;

        if ($this -> presence()) {
            //Update
            $pdoconn -> beginTransaction();
            $st = $pdoconn -> prepare("UPDATE Account SET password = :password WHERE accName = :accName");
            $input = array(
                ':accName' => $this->accName,
                ':password' => $this->password
            );
        } else {
            //Create
            $pdoconn -> beginTransaction();
            $pdoconn -> query("INSERT INTO Employees SET empFname = NULL");

            $st1 = $pdoconn -> prepare("SELECT MAX(empID) AS emp FROM Employees");
            $st1 -> execute();
            $result = $st1 -> fetchColumn();

            $st = $pdoconn -> prepare("INSERT INTO Account VALUES(:empID, :accName, :password)");
            $input = array(
                ':empID' => $result,
                ':accName' => $this->accName,
                ':password' => $this->password
            );
        }
        $st -> execute($input);
        $pdoconn -> commit();
        return $st -> rowCount();
    }

    public function delete(){
        global $pdoconn;
        if ($this -> presence()){
            $pdoconn -> beginTransaction();
            $st = $pdoconn -> prepare("DELETE FROM Account WHERE accName = :accName ");
            $st -> execute(array(
                ':accName' => $this->accName
            ));
            $pdoconn -> commit();
            return $st ->rowCount();
        } else {
            return 0;
        }
    }

    public function saveEmployeeEntity() {
        //Update
        global $pdoconn;
        $input = array(
            ':accName' => $this -> accName,
            ':empStatus' => $this -> empStatus
        );
        $pdoconn -> beginTransaction();
        $st = $pdoconn -> prepare("SELECT empID FROM Account WHERE accName = :accName");
        $st -> execute($input);
        $emID = $st-> fetchColumn();
        $input[':empID'] = $emID;

        $st = $pdoconn -> prepare("UPDATE Employees SET empStatus = :empStatus WHERE empID = :empID");
        $st -> execute($input);
        $pdoconn -> commit();
        return $st -> rowCount();
    }
}

